<template>
  <div id="loginTpl">

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item>
        <img src="../assets/tou.gif" class="login-img">
      </el-form-item>
      <el-form-item prop="username">
        <el-input v-model="ruleForm.username" placeholder="请输入用户名" prefix-icon="icon-user-check"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" v-model="ruleForm.password" placeholder="请输入密码" prefix-icon="icon-key"
        @keydown.enter.native="loginSubmit('ruleForm')"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loginSubmit('ruleForm')">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
// import { checkLogin } from "../login";
import { mapState } from "vuex";
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        userpwd: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  computed: {
    ...mapState({
      token: state => state.login.token
    })
  },
  created() {
    this.isLogin();
  },
  methods: {
    isLogin() {
      if (!this.token) this.$router.push("/home");
    },
    loginSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$store
            .dispatch("checkLogin", this.ruleForm)
            .then(res => {
              console.log(res);
              if (res.meta.status == 200) {
                localStorage.setItem("myToken", this.token);
                this.$message({
                  message: res.meta.msg,
                  type: "success"
                });
                this.$router.push("/home");
              }
            })
            .catch((err) =>
              this.$message({
                message: err.meta.msg,
                type: "warning"
              })
            );
        }
      });
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style  lang="scss">
#loginTpl {
  width: 100%;
  height: 100%;
  background: rgb(171, 217, 235);
  // background: url("../assets/16.jpg") ;
  position: fixed;
  > form {
    opacity: 0.8;
    position: absolute;
    left: 50%;
    top: 50%;
    width: 600px;
    height: 400px;
    transform: translate(-50%, -50%);
    border-radius: 20px;
    background: url("../assets/bg.jpg") no-repeat center;
    background-size: cover;
    .login-img {
      position: relative;
      left: 0;
      width: 200px;
      height: 200px;
      margin-left: 110px;
      margin-top: -100px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid rgb(255, 184, 52);
      box-shadow: 0 1px 5px rgb(61, 57, 57);
      overflow: hidden;
    }
    .el-input {
      width: 400px;
    }
    .el-input--prefix .el-input__inner {
      padding-left: 40px;
    }
    .icon-user-check:before,
    .icon-key:before {
      font-size: 25px;
      position: absolute;
      bottom: 5px;
      left: 5px;
      color: #dcdfe6;
    }
    .el-button {
      width: 200px;
      margin-left: 100px;
    }
  }
}
</style>
